﻿using System;
using System.Collections.Generic;
using System.Net.Http.Headers;
using System.Net.Http;
using System.Threading.Tasks;
using XiAnAirport.DAL;
using XiAnAirport.Models;
using System.IO;
using System.Net;
using System.Text;

namespace XiAnAirport.BUS
{
    public class LogBUS
    {
        public static string ServerIp = string.Empty;
        public ResponseMessage InsertErrorLog(ErrorLogInfo errorLogInfo)
        {
            ResponseMessage message = new ResponseMessage();
            try
            {
                LogDAL logDAL = new LogDAL();
                bool result = logDAL.AddErrorLog(errorLogInfo);
                if (result)
                {
                    message.errcode = 0;
                }
                else
                {
                    message.errcode = 1;
                }
            }
            catch
            {
                message.errcode = 1;
            }

            return message;
        }

        public ResponseMessage QueryErrorLog(ErrorLogInfo errorLogInfo)
        {
            ResponseMessage message = new ResponseMessage();
            try
            {
                LogDAL logDAL = new LogDAL();
                message.errcode = 0;
                message.data = logDAL.QueryErrorLog(errorLogInfo);
                message.total = logDAL.QueryErrorLogCount(errorLogInfo);
            }
            catch
            {
                message.errcode = 1;
                message.errmsg = "查询异常";
            }
            return message;
        }

        public ResponseMessage InsertOperationLog(OperationLog operationLog)
        {
            ResponseMessage message = new ResponseMessage();
            LogDAL logDAL = new LogDAL();
            try
            {
                bool result = logDAL.AddOperationLog(operationLog);
                if (result)
                {
                    message.errcode = 0;
                }
                else
                {
                    message.errcode = 1;
                }
            }
            catch
            {
                message.errcode = 1;
            }

            return message;
        }

        public ResponseMessage QueryOperationLog(OperationLog operationLog)
        {
            ResponseMessage message = new ResponseMessage();
            try
            {
                LogDAL logDAL = new LogDAL();
                message.errcode = 0;
                message.data = logDAL.QueryOperationlog(operationLog);
                message.total = logDAL.QueryOperationlogCount(operationLog);
            }
            catch
            {
                message.errcode = 1;
                message.errmsg = "查询异常";
            }
            return message;
        }

        public ResponseMessage InsertClearLog(ClearLog clearLog)
        {
            ResponseMessage message = new ResponseMessage();
            try
            {
                LogDAL logDAL = new LogDAL();
                bool result = logDAL.AddClearLog(clearLog);
                if (result)
                {
                    message.errcode = 0;
                }
                else
                {
                    message.errcode = 1;
                }
            }
            catch (Exception ex)
            {
                message.errcode = 1;
            }
          
            return message;
        }

        public ResponseMessage QueryClearLog(ClearLog clearLog)
        {
            ResponseMessage message = new ResponseMessage();
            try
            {
                LogDAL logDAL = new LogDAL();
                message.errcode = 0;
                message.data = logDAL.QueryClearLog(clearLog);
                message.total = logDAL.QueryClearLogCount(clearLog);
            }
            catch
            {
                message.errcode = 1;
                message.errmsg = "查询异常";
            }
            return message;
        }

        public ResponseMessage InsertLoginLog(LoginLog loginLog)
        {
            ResponseMessage message = new ResponseMessage();
            try
            {
                LogDAL logDAL = new LogDAL();
                bool result = logDAL.AddLoginLog(loginLog);
                if (result)
                {
                    message.errcode = 0;
                }
                else
                {
                    message.errcode = 1;
                }
            }
            catch (Exception ex)
            {
                message.errcode = 1;
            }

            return message;
        }

        public ResponseMessage QueryLoginLog(LoginLog loginLog)
        {
            ResponseMessage message = new ResponseMessage();
            try
            {
                LogDAL logDAL = new LogDAL();
                message.errcode = 0;
                message.data = logDAL.QueryLoginLog(loginLog);
                message.total = logDAL.QueryLoginLogCount(loginLog);
            }
            catch
            {
                message.errcode = 1;
                message.errmsg = "查询异常";
            }
            return message;
        }

        public ResponseMessage InsertLockLog(LockLog lockLog)
        {
            ResponseMessage message = new ResponseMessage();
            try
            {
                LogDAL logDAL = new LogDAL();
                bool result = logDAL.AddLockLog(lockLog);
                if (result)
                {
                    message.errcode = 0;
                }
                else
                {
                    message.errcode = 1;
                }
            }
            catch (Exception ex)
            {
                message.errcode = 1;
            }

            return message;
        }

        public ResponseMessage QueryLockLog(LockLog lockLog)
        {
            ResponseMessage message = new ResponseMessage();
            try
            {
                LogDAL logDAL = new LogDAL();
                message.errcode = 0;
                message.data = logDAL.QueryLockLog(lockLog);
                message.total = logDAL.QueryLockLogCount(lockLog);
            }
            catch
            {
                message.errcode = 1;
                message.errmsg = "查询异常";
            }
            return message;
        }

        public string HttpClientPost(string url, object datajson)
        {
            var httpClientHandler = new HttpClientHandler();
            httpClientHandler.ServerCertificateCustomValidationCallback = (message, cert, chain, errors) => { return true; };
            HttpClient httpClient = new HttpClient(httpClientHandler);//http对象
            //表头参数
            httpClient.DefaultRequestHeaders.Accept.Clear();
            httpClient.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json"));

            //转为链接需要的格式
            HttpContent httpContent = new JsonContent(datajson);
            //请求
            HttpResponseMessage response = httpClient.PostAsync(url, httpContent).Result;
            if (response.IsSuccessStatusCode)
            {
                Task<string> t = response.Content.ReadAsStringAsync();
                if (t != null)
                {
                    return t.Result;
                }
            }
            return "";
        }

        public string Post(string url)
        {
            string result = "";
            HttpWebRequest req = (HttpWebRequest)WebRequest.Create(url);
            req.Method = "POST";
            HttpWebResponse resp = (HttpWebResponse)req.GetResponse();
            Stream stream = resp.GetResponseStream();
            //获取内容
            using (StreamReader reader = new StreamReader(stream, Encoding.UTF8))
            {
                result = reader.ReadToEnd();
            }
            return result;
        }
    }
}
